package com.c2b.algorithm.leetcode.base;

/**
 * <a href='https://leetcode.cn/problems/number-of-arithmetic-triplets/'>算术三元组的数目(Number of Arithmetic Triplets)</a>
 * <p>
 * 给你一个下标从 0 开始、严格递增 的整数数组 nums 和一个正整数 diff 。如果满足下述全部条件，则三元组 (i, j, k) 就是一个 算术三元组：
 *     <ul>
 *         <li>i < j < k ，</li>
 *         <li>nums[j] - nums[i] == diff 且 nums[k] - nums[j] == diff</li>
 *     </ul>
 * </p>
 * <p>返回不同 算术三元组 的数目。</p>
 *
 * <p>
 * <b>示例</b>
 * <pre>
 * 示例 1：
 *      输入：nums = [0,1,4,6,7,10], diff = 3
 *      输出：2
 *      解释：
 *          (1, 2, 4) 是算术三元组：7 - 4 == 3 且 4 - 1 == 3 。
 *          (2, 4, 5) 是算术三元组：10 - 7 == 3 且 7 - 4 == 3 。
 *
 * 示例 2：
 *      输入：nums = [4,5,6,7,8,9], diff = 2
 *      输出：2
 *      解释：
 *          (0, 2, 4) 是算术三元组：8 - 6 == 2 且 6 - 4 == 2 。
 *          (1, 3, 5) 是算术三元组：9 - 7 == 2 且 7 - 5 == 2 。
 * </pre>
 * </p>
 *
 * <p>
 * <b>提示：</b>
 *  <ul>
 *      <li>3 <= nums.length <= 200</li>
 *      <li>0 <= nums[i] <= 200</li>
 *      <li>1 <= diff <= 50</li>
 *      <li>nums 严格 递增</li>
 *  </ul>
 * </p>
 *
 * @author c2b
 * @since 2024/1/24 14:53
 */
public class LC2367NumberOfArithmeticTriplets_S {

    static class Solution {
        public int arithmeticTriplets(int[] nums, int diff) {
            int res = 0;
            int[] cnt = new int[201];
            for (int num : nums) {
                cnt[num] = 1;
            }
            for (int i = 0; i < cnt.length; i++) {
                if (i + 2 * diff >= cnt.length) {
                    break;
                }
                if (cnt[i] == 1 && cnt[i + diff] == 1 && cnt[i + 2 * diff] == 1) {
                    ++res;
                }
            }
            return res;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.arithmeticTriplets(new int[]{0, 1, 4, 6, 7, 10}, 3));
        System.out.println(solution.arithmeticTriplets(new int[]{4, 5, 6, 7, 8, 9}, 2));
        System.out.println(solution.arithmeticTriplets(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200}, 1));
    }
}
